package com.t235.gc.utils;



import com.t235.gc.devicevo.DeviceVo;
import com.t235.gc.entity.Device;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.util.List;

public class ExcelUtils {
    public static void export(List<DeviceVo> dataList, HttpServletResponse response) throws Exception {
        Workbook wb=new HSSFWorkbook();//新建一个工作簿
        /*
         * 导出所以用到输出流
         *参数为输出的地址
         */
//        FileOutputStream fout=new FileOutputStream("F:\\poi.xls");
        response.setContentType("octets/stream");
        response.addHeader("Content-Disposition", "attachment;filename="+System.currentTimeMillis()+".xls");
        ServletOutputStream fout = response.getOutputStream();
        Sheet sheet= wb.createSheet("第一个sheet页");//创建一个sheet页
        Row row0 =sheet.createRow(0);
        row0.createCell(0).setCellValue("设备编号");
        row0.createCell(1).setCellValue("设备名称");
        row0.createCell(2).setCellValue("二维码");
        row0.createCell(3).setCellValue("场地名称");
        row0.createCell(4).setCellValue("设备地址");
        row0.createCell(5).setCellValue("所属分拣中心");
        row0.createCell(6).setCellValue("所属运营商");
        row0.createCell(7).setCellValue("所属合伙人");
        row0.createCell(8).setCellValue("满溢度");
        row0.createCell(9).setCellValue("商品库存");
        row0.createCell(10).setCellValue("设备状态");
        row0.createCell(11).setCellValue("营业状态");
        row0.createCell(12).setCellValue("创建时间");
        for (int i = 1; i < dataList.size(); i++) {
            DeviceVo deviceVo = dataList.get(i);
            Row row=sheet.createRow(i); // 创建一个行 第一行
            int j = 0;
            row.createCell(j++).setCellValue(deviceVo.getDeviceid());
            row.createCell(j++).setCellValue(deviceVo.getDevicename());
            row.createCell(j++).setCellValue(deviceVo.getDeviceqrcode());
            row.createCell(j++).setCellValue(deviceVo.getVenuesname());
            row.createCell(j++).setCellValue(deviceVo.getDz());
            row.createCell(j++).setCellValue(deviceVo.getSortingname());
            row.createCell(j++).setCellValue(deviceVo.getOperatorsname());
            row.createCell(j++).setCellValue(deviceVo.getPartnersName());
            row.createCell(j++).setCellValue(deviceVo.getSpillover());
            row.createCell(j++).setCellValue(deviceVo.getIsstock());
            row.createCell(j++).setCellValue(deviceVo.getDevicestatus());
            row.createCell(j++).setCellValue(deviceVo.getIsflag());
            row.createCell(j++).setCellValue(deviceVo.getCtime());
        }


        wb.write(fout);//Workbook提供了write的方法
        fout.close();//将输出流关闭

    }
}
